﻿<%@ Page Language="C#" %>

<%@ Import Namespace="IRL.Utilities" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>


<script runat="server">
    public const int CURRENT_MODULE = 51;
    
    int pid, oid;
    bool HasWriteAccess = false;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        Security SysAcc = new Security(CURRENT_MODULE);
        // check is authenticate
        SysAcc.Authenticate();
        HasWriteAccess = SysAcc.IsAuthorized(2);
        
        pid = Convert.ToInt32(Request.QueryString["pid"]);
        oid = Convert.ToInt32(Request.QueryString["oid"]);

        if (!Page.IsPostBack)
        {
            headerInfo();
            rebind();
            // set back history
            ViewState.Add("BackStep", 0);
        }
        else
        {
            ViewState["BackStep"] = (int)ViewState["BackStep"] + 1;
        }
        
    }

    void rebind()
    {
        if (HasWriteAccess)
        {
            List.fillList(cboContact, "SELECT [Contact_ID], [Title]+'. '+[FirstName]+' '+[LastName] AS [Fullname] FROM [tbl_Contact_Info] WHERE [Company_ID]=" + oid);
            //List.fillList(cboContact, "SELECT [Contact_ID], [Title]+'. '+[FirstName]+' '+[LastName] AS [Fullname] FROM [tbl_Contact_Person] WHERE [Company_ID]=" + oid);
        }
        else
        {
            divForm.Visible = false;
        }
        
        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            SqlCommand cmd = new SqlCommand("SELECT pct.[ContactID], [Title]+'. '+[FirstName]+' '+[LastName] AS [Fullname], [JobTitle], [Phone], [Email] FROM [tbl_Contact_Info] ct INNER JOIN [cms_ProjectContacts] pct ON ct.[Contact_ID]=pct.[ContactID] WHERE [ProposalID]=" + pid + " AND [Company_ID]=" + oid, conn);
            //SqlCommand cmd = new SqlCommand("SELECT pct.[ContactID], [Title]+'. '+[FirstName]+' '+[LastName] AS [Fullname], [JobTitle], [Mobile], [Email_Address] FROM [tbl_Contact_Person] ct INNER JOIN [cms_ProjectContacts] pct ON ct.[Contact_ID]=pct.[ContactID] WHERE [ProposalID]=" + pid + " AND [Company_ID]=" + oid, conn);
            conn.Open();
            // bound to datagrid
            SqlDataReader dr = cmd.ExecuteReader();
            grid1.DataSource = dr;
            grid1.DataBind();      
            
            // no contact person in list
            if (cboContact.Items.Count == 0)
            {
                cmdAddContact.Visible = false;
            }
                // add link for Add new
                lnkAddContact.HRef = "saveContact.aspx?pid=" + pid + "&oid=" + oid;
                lnkAddContact.Visible = true;
                
            //}
        }
    }

    void headerInfo()
    {
        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            SqlCommand cmd = new SqlCommand("sp_ProjectInfo", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@proposalid", SqlDbType.Int).Value = pid;
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    ViewState.Add(dr.GetName(i), dr[i]);
                }
                dr.Close();
                // get lookup
                ViewState["ClientName"] = Lookup.GetText("SELECT [Company_Name] FROM [vw_Clients] WHERE [Company_ID]=" + ViewState["ClientID"].ToString(), conn);
                ViewState["ProjectType"] = Lookup.GetText("sp_Select_cms_Lookup 'project_type', " + ViewState["ProjectType"].ToString() + ", 1", conn);
                ViewState["ResearchType"] = Lookup.GetText("sp_Select_cms_Lookup 'research_type', " + ViewState["ResearchType"].ToString() + ", 1", conn);
                ViewState["ResearchCategory"] = Lookup.GetText("sp_Select_cms_Lookup 'research_category', " + ViewState["ResearchCategory"].ToString() + ", 1", conn);
            }
        }
    }
    
    protected void cmdAddContact_Click(object sender, EventArgs e)
    {
        int contactID = Convert.ToInt32(cboContact.SelectedValue);

        if (HasWriteAccess)
        {
            object connStr = ConfigurationSettings.AppSettings["sqlConn"];
            using (SqlConnection conn = new SqlConnection(connStr.ToString()))
            {
                SqlCommand cmd = new SqlCommand("sp_SaveProjectContact", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@proposalid", SqlDbType.Int).Value = pid;
                cmd.Parameters.Add("@contactid", SqlDbType.Int).Value = contactID;
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            rebind();
        }
    }

    protected void grid1_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Remove":
                if (HasWriteAccess)
                {
                    DeleteItem(e);
                }
                break;
                
            default:
                // Do nothing
                break;
        }
        rebind();
    }
    void DeleteItem(DataGridCommandEventArgs e)
    {
        string contactID = e.Item.Cells[0].Text;
        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            SqlCommand cmd = new SqlCommand("DELETE FROM [cms_ProjectContacts] WHERE [ProposalID]=" + pid + " AND [ContactID]=" + contactID, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }

    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <title>Indochina Research</title>
    <link rel="SHORTCUT ICON" href="../irl.ico" type="image/x-icon" />
    <link href="../css/global.css" rel="stylesheet" type="text/css" />
    <link href="css/cms.css" rel="stylesheet" type="text/css" />    
        
    
</head>
<body>
<table width="773" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td width="765" height="540" align="center" valign="top" style="border:solid 1px #10147e;" >
			<!-- header -->
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr bgcolor="#F4F4F9">
					<td colspan="3" align="right" style="padding:10px;">
						<b class="blueL">Client Management System</b>
					</td>
				</tr>
				<tr>
					<td class="wave_l">&nbsp;</td>
					<td><img src="images/wave_logo.jpg" width="107" height="83"></td>
					<td class="wave_r">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="3" style="padding:0 20px;">
						<div style="background-color:#bcb3d5; padding:5px 10px;">
							<span style="float:right">
							    <!--<a class="blue" href="javascript:history.go(-<%=(int)ViewState["BackStep"]+1 %>);">&laquo; Back</a>-->
							    <a class="blue" href="listProjects.aspx">&laquo; Back</a>
							</span>
							<span class="pathmap">
								<a href="default.aspx" >CPanel</a> :: 
								<a href="default2.aspx" >Ops Files</a> :: Contact Person(s)
						  </span>
						</div>
						<br />
						
						
					</td>
				</tr>
				
			</table>
			
			<!-- body -->
			<form id="DataForm" method="post" runat="server">
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td align="center" valign="top">
						<table border="0" cellspacing="0" cellpadding="5" align="center" >
                            <tr>
                                <td align="right">Project name: </td>
                                <td class="blueL"><a class="edit" href="saveProject.aspx?pid=<%=pid.ToString() %>"><%=ViewState["ProjectName"].ToString() %></a></td>
                                <td width="40">&nbsp;</td>
                                <td align="right">Client name: </td>
                                <td class="blueL"><%=ViewState["ClientName"].ToString() %></td>
                            </tr>
                            <tr>
                                <td align="right">Project type: </td>
                                <td class="blueL"><%=ViewState["ProjectType"].ToString() %></td>
                                <td width="40">&nbsp;</td>
                                <td align="right">Project code: </td>
                                <td class="blueL"><%=ViewState["ProjectCode"].ToString() %></td>
                            </tr>
                            <tr>
                                <td align="right">Research type: </td>
                                <td class="blueL"><%=ViewState["ResearchType"].ToString() %></td>
                                <td width="40">&nbsp;</td>
                                <td align="right">Research category: </td>
                                <td class="blueL"><%=ViewState["ResearchCategory"].ToString() %></td>
                            </tr>
                            <tr>
                                <td colspan="5">&nbsp;
                                    
                                </td>
                            </tr>
                        </table>
                        <!-- Form -->
                        <div align="center">
                            <div id="divForm" class="divInfo" runat="server">
                                <fieldset>
                                    <legend><b style="font-size: 7pt;">Add contact person &nbsp; </b></legend>
                                    <table border="0" cellspacing="0" cellpadding="6">
                                        <tr>
                                            <td>&nbsp;</td>
                                            <td>Fullname:</td>
                                            <td>&nbsp;</td>
                                            <td><asp:DropDownList ID="cboContact" runat="server" /></td>
                                            <td>&nbsp;</td>
                                            <td>
                                                <asp:Button ID="cmdAddContact" Text="Add" runat="server" OnClick="cmdAddContact_Click" />
                                                <a id="lnkAddContact" visible="false" runat="server">Add new</a></td>
                                        </tr>
                                    </table>
                                </fieldset>
                            </div>
                            <br />
                        </div>
                        <!-- Data Grid -->
                        <asp:DataGrid ID="grid1" runat="server" CellSpacing="1" CellPadding="4" BorderWidth="0"
                            GridLines="none" HorizontalAlign="center" AutoGenerateColumns="false" OnItemCommand="grid1_ItemCommand">
                            <HeaderStyle BackColor="#bcb3d5" Font-Size="7.5pt" ForeColor="white" Font-Bold="true" />
                            <ItemStyle BackColor="#efefef" />
                            <AlternatingItemStyle BackColor="#ffffff" />
                            <Columns>
                                <asp:BoundColumn Visible="false" DataField="ContactID" />
                                
                                <asp:TemplateColumn HeaderText="No">
                                    <HeaderStyle Width="20" />
                                    <ItemTemplate>
                                        <%# Container.ItemIndex+1%>
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Fullname">
                                    <HeaderStyle Width="120" />
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "FullName")%>
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                                <asp:BoundColumn HeaderText="Job Title" DataField="JobTitle">
                                    <HeaderStyle Width="200" />
                                </asp:BoundColumn>
                                <asp:BoundColumn HeaderText="Mobile" DataField="Phone">
                                    <HeaderStyle Width="100" />
                                </asp:BoundColumn>
                                <asp:TemplateColumn HeaderText="Email">
                                    <HeaderStyle Width="180" />
                                    <ItemTemplate>
                                        <a href="mailto:<%# DataBinder.Eval(Container.DataItem, "Email")%>">
                                            <%# DataBinder.Eval(Container.DataItem, "Email")%>
                                        </a>
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                                
                                <asp:TemplateColumn HeaderText="Remove" >
                                    <HeaderStyle Width="40" HorizontalAlign="center" />
                                    <ItemStyle HorizontalAlign="center" />
                                    <ItemTemplate>
                                        <asp:ImageButton ID="cmdRemove" CommandName="Remove" ImageUrl="images/delete.gif"
                                            runat="server" />
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                            </Columns>
                        </asp:DataGrid>
					</td>
				</tr>
			</table>
			</form>
			
		</td>
		
		<td width="6" class="rightShadow" valign="top">
			<img src="images/white.gif" width="6" height="6" /></td>
	</tr>
	
	<!-- bottom shadow -->
	<tr>
		<td height="6" class="bottomShadow">
			<img src="images/white.gif" width="6" height="6" /></td>
		
		<td width="6" height="6">
			<img src="images/shadow_corner.gif" width="6" height="6" /></td>
	</tr>
</table>

    
</body>
</html>
